//
//  WJHomeViewController.swift
//  WJWeiBo
//
//  Created by wangjun on 16/7/19.
//  Copyright © 2016年 wangjun. All rights reserved.
//

import UIKit
// cell 可重用标识符
private let homeCellId = "homeCellId"
class WJHomeViewController: WJVisitorViewController {

    // 全局的模型数组
    lazy var statusListViewModel: WJStatusListViewModel = WJStatusListViewModel()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        if !isLogin {
            visitorView?.setVisitorInfo(nil, title: nil)
            return
        }
        setupTableViewInfo()
        getHomeData()
    }
    // 设置tableView
    func setupTableViewInfo(){
        navigationController!.view.insertSubview(pullDownTipLabel, belowSubview: navigationController!.navigationBar)
        // 设置数据源代理
        tableView.dataSource = self
        tableView.delegate = self
        // 注册cell
        tableView.registerClass(WJStautsCell.self, forCellReuseIdentifier: homeCellId)
        // 设置tableViewCell高度
        //tableView.rowHeight = 200
        // 设置tableView 的cell的预估行高 (预估行高越接近真实cell高度越好)
        tableView.estimatedRowHeight = 200
        tableView.tableFooterView = indicatorView
        tableView.addSubview(refreshControl)
        refreshControl.addTarget(self, action: "refreshAction", forControlEvents: UIControlEvents.ValueChanged)
        
        //cell底部分割线去掉1>.2>.
        tableView.separatorStyle = .None
//        tableView.separatorColor = UIColor.whiteColor()
//        cell顶部有灰色的间距
        
    }
    func refreshAction() {
        
        getHomeData()
    }
    
//    MARK: -懒加载控件:
        private lazy var indicatorView: UIActivityIndicatorView = {
            let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
            view.color = ThemeColor
            return view
        }()
 //  MARK: -懒加载控件:
    private lazy var refreshControl: SHRefreshControl = SHRefreshControl()
    // 请求微博数据提示
    private lazy var pullDownTipLabel: UILabel = {
        let lab = UILabel(textColor: UIColor.whiteColor(), textFont: 12)
        lab.frame = CGRect(x: 0, y: 64 - 30, width: SCREENW, height: 30)
        lab.textAlignment = .Center
        lab.backgroundColor = ThemeColor
        lab.hidden = true
        return lab
    }()

    
}
// MARK: - UITableViewDataSource
extension WJHomeViewController: UITableViewDataSource,UITableViewDelegate {
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListViewModel.statusList.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(homeCellId, forIndexPath: indexPath) as! WJStautsCell
        // 给cell赋值
        cell.statusViewModel = statusListViewModel.statusList[indexPath.row]
        return cell
    }
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if indexPath.row == statusListViewModel.statusList.count - 1 && !indicatorView.isAnimating() {
            
            indicatorView.startAnimating()
            getHomeData()
            
        }
    }
    
    
}

// MARK: - 请求微博数据
extension WJHomeViewController {
    // 请求首页数据
    func getHomeData(){
        statusListViewModel.loadHomeData(indicatorView.isAnimating()) { (isSuc,count) -> () in
           
            self.endRefreshing()
            if !isSuc {
                print("请求数据失败")
                return
            }
            self.tableView.reloadData()
            // 下拉刷新
            if count >= 0{
                self.setpullDownTipLabelAnim(count)
            }
        }
    }
    
    
    // 设置微博提示动画
    func setpullDownTipLabelAnim(count: Int){
        // 判断如果当前在动画也就是显示 就不在进行动画
        if pullDownTipLabel.hidden == false{
            return
        }
        // 显示空间
        pullDownTipLabel.hidden = false
        var name = ""
        if count == 0 {
            name = "您已经是最新的微博数据啦"
        }else {
            name = "您更新了\(count)条微博"
        }
        // 设置text
        pullDownTipLabel.text = name
        
        // 设置动画
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            
            self.pullDownTipLabel.transform = CGAffineTransformMakeTranslation(0, 30)
            
            }) { (_) -> Void in
                UIView.animateWithDuration(0.5, delay: 1, options: [], animations: { () -> Void in
                    self.pullDownTipLabel.transform = CGAffineTransformIdentity
                    }, completion: { (_) -> Void in
                        // 完成后隐藏
                        self.pullDownTipLabel.hidden = true
                })
        }
    }
    // 停止各种动画
    func endRefreshing(){
        self.indicatorView.stopAnimating()
        self.refreshControl.endRefreshing()
    }
            
        }
        

